package com.ruoyi.common.utils.uuid;

import com.alibaba.excel.annotation.ExcelProperty;
import com.ruoyi.common.annotation.ConditionalExport;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.util.*;

import java.lang.reflect.*;
import java.util.Map;

/*public class DynamicExcelExporter {
    public static Object processAnnotations(Object vo, Map<String, String> params) {
        return Proxy.newProxyInstance(
            vo.getClass().getClassLoader(),
            vo.getClass().getInterfaces(),
            (proxy, method, args) -> {
                // 处理字段注解
                Field[] fields = vo.getClass().getDeclaredFields();
                for (Field field : fields) {
                    ConditionalExport cond = field.getAnnotation(ConditionalExport.class);
                    if (cond != null) {
                        String paramValue = params.get(cond.paramName());
                        boolean shouldExport = cond.expectedValue().equals(paramValue);

                        if (!shouldExport) {
                            // 如果不需要导出，则在代理中忽略该字段
                            if (field.getName().equals(method.getName())) {
                                return null;
                            }
                        }
                    }
                }
                // 其他方法调用正常处理
                try {
                    return method.invoke(vo, args);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
        );
    }
} */
public class DynamicExcelExporter {
    /*public static Map<String, Object> processToMap(Object vo, Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();
        Field[] fields = vo.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            ConditionalExport cond = field.getAnnotation(ConditionalExport.class);
            if (cond == null || params.get(cond.paramName()).equals(cond.expectedValue())) {
                try {
                    result.put(field.getName(), field.get(vo));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return result;
    } */

    public static Map<String, Object> processToMap(Object vo, Map<String, String> params) {
        Map<String, Object> result = new LinkedHashMap<>(); // 保持字段顺序
        Field[] fields = vo.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            ConditionalExport cond = field.getAnnotation(ConditionalExport.class);

            if (cond == null) { // 无条件注解的字段直接保留
                try {
                    result.put(field.getName(), field.get(vo));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            } else if (params.get(cond.paramName()).equals(cond.expectedValue())) { // 满足条件才保留
                try {
                    result.put(field.getName(), field.get(vo));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return result;
    }

}





 /* public static void processAnnotations(Object vo, Map<String, String> params) {
        Field[] fields = vo.getClass().getDeclaredFields();
        for (Field field : fields) {
            ConditionalExport cond = field.getAnnotation(ConditionalExport.class);
            if (cond != null) {
                String paramValue = params.get(cond.paramName());
                boolean shouldExport = cond.expectedValue().equals(paramValue);

                if (!shouldExport) {
                    ExcelProperty excelProp = field.getAnnotation(ExcelProperty.class);
                    if (excelProp != null) {
                        // 移除@ExcelProperty注解
                        field.setAccessible(true);
                        Annotation[] newAnnotations = Arrays.stream(field.getAnnotations())
                            .filter(a -> !(a instanceof ExcelProperty))
                            .toArray(Annotation[]::new);
                        field.setAnnotations(newAnnotations);
                    }
                }
            }
        }
    }*/

